#include "ccd290_extract.h"

// 采用offset binary编码
//
//  @2022-05-23:更新命令参数功能设计，如果不通过-k选项指定.keyword文件，bin2fits就自动搜索与ccd_data.bin
//              对应的ccd_data.info

int main( int argc, char *argv[] ) {

    ccd290_config cfg;
    
    char bin_file[1024];    // raw格式（二进制）数据文件名
	char bin_root[256];
    int has_bin_file = 0;

    char fits_file[1024];   // fits文件名
    int has_fits_file = 0;

    char conf_file[1024];   // CCD读出配置参数文件名
    int has_conf_file = 0;

    char key_file[1024];     // fits关键字文件名
    int has_key_file = 0;

    int help_flag = 0;


    int opt;
    opterr = 1;
    while( (opt=getopt(argc,argv,"b:f:c:k:h")) != -1 ){
        switch(opt){
            case 'b':
                has_bin_file = 1;
				sprintf(bin_root,"%s", optarg);
//                sprintf(bin_file, "%s.bin", bin_root);
                break;
            case 'f':
                has_fits_file = 1;
                sprintf(fits_file, "%s", optarg);
                break;
            case 'c':
                has_conf_file = 1;
                sprintf(conf_file, "%s", optarg);
                break;
            case 'k':
                has_key_file = 1;
                sprintf(key_file, "%s", optarg);
                break;
            case 'h':
            	help_flag = 1;
            	break;
            default:
                printf("other option:%c\n", opt);
        }
    }
    
    if( help_flag == 1 ) {
    	printf("usage: %s -b binary_data -f out.fits -c ccd.config -k keywords.info[-h]\n", argv[0]);
    	return 0;
    }

    if( has_bin_file != 1 ){
        printf("--> binary data file is not provided!\n");
        return 0;
    } else {
        if( find_postfix(bin_root,".bin") == 1 ){
            sprintf(bin_file, "%s", bin_root);
        } else {
            sprintf(bin_file, "%s.bin", bin_root);
        }
    }


    if( has_fits_file != 1 ) {
	// process fits filename, including path
//		replace_postfix( bin_file, ".bin", ".fits", fits_file );
//		printf("--> output fits filename is: %s\n", fits_file);

        if( find_postfix(bin_root, ".bin") == 1 ){
            replace_postfix( bin_file, ".bin", ".fits", fits_file );
        } else {
		    sprintf(fits_file, "%s.fits", bin_root);
        }
	}

	printf("--> output fits filename is: %s\n", fits_file);


    if( has_conf_file == 1 ){
        printf("--> loading config from: %s\n", conf_file);
        init_ccd290_config_from_file( &cfg, conf_file);        
    } else {
        printf("--> reading CDS config from: ccd290_config.ini\n");
        init_ccd290_config_from_file( &cfg, "ccd290_config.ini" );
        // init_ccd290_config_to_defaults( &cfg );
    }

    // When opt -k is not specified, bin2fits will try to find ccd_data.info (corresponds to ccd_data.bin)
    if( has_key_file == 0 ){
        
        char info_file[1024];
        replace_postfix(bin_file, ".bin", ".info", info_file);
        printf("**> no fits keyword file provided, trying to find %s\n", info_file);

        FILE *fp_info = NULL;
        fp_info = fopen(info_file, "r");

        if( fp_info == NULL ){
            printf("**> failed to find %s\n", info_file);
            printf("**> setting key_file to *NULL!\n");
            sprintf(key_file,"NULL");
        } else {
            printf("--> sucessfully found %s\n", info_file);
            printf("--> setting key_file to %s!\n", info_file);
            sprintf(key_file, "%s", info_file);
            fclose(fp_info);
        }
    }

    if( cfg.use_csst_format == 0 ){

        if( cfg.raw_dtype_is_float == 0 ){
            extract_ccd290_16ch_uint16( bin_file, 
                                        fits_file, 
                                        key_file,
                                        &cfg );
        } else if( cfg.raw_dtype_is_float == 1 ){
            extract_ccd290_16ch_flt( bin_file, 
                                     fits_file, 
                                     key_file,
                                     &cfg );
        }
    } else if( cfg.use_csst_format == 1 ){
        extract_ccd290_16ch_csst( bin_file, 
                                  fits_file,
                                  key_file,
                                  &cfg );
    }

    return 0;

}
